/**
 * 日志存储
 */

const logger4js = require("log4js");
const levels = {
  trace: logger4js.levels.TRACE,
  debug: logger4js.levels.DEBUG,
  info: logger4js.levels.INFO,
  warn: logger4js.levels.WARN,
  error: logger4js.levels.ERROR,
  fatal: logger4js.levels.FATAL,
};

logger4js.configure({
  appenders: {
    console: { type: "console" },
    info: { type: "file", filename: "log/all-logs.log" },
    error: {
      type: "dateFile",
      filename: "logs/log",
      pattern: "yyyy-MM-dd.log",
      alwaysIncludePattern: true,   // 设置文件名称 filename + pattern
    },
  },
  categories: {
    default: { appenders: ["console"], level: "debug" },
    error: { appenders: ["error", "console"], level: "error" },
    info: { appenders: ["info", "console"], level: "info" },
  },
});

/**
 * 日志输出 level 为 debug
 * @param {string} content
 */
exports.debug = (content) => {
  let logger = logger4js.getLogger('debug');
  logger.level = levels.debug;
  logger.debug(content);
};

/**
 * 日志输出 level 为 error
 * @param {string} content
 */
exports.error = (content) => {
  let logger = logger4js.getLogger('error');
  logger.level = levels.error;
  logger.error(content);
};

/**
 * 日志输出 level 为 info
 * @param {string} content
 */
exports.info = (content) => {
  let logger = logger4js.getLogger('info');
  logger.level = levels.info;
  logger.info(content);
};
